import { segment } from "oicq";
import plugin from '../../lib/plugins/plugin.js';
import fetch from "node-fetch";
import {createRequire} from "module";
const require = createRequire(import.meta.url);
const botname = global.Bot.nickname;   //bot名字，自适应的不用改
const _path = process.cwd(); //系统路径
let no = '哒咩!' //拒绝非主人使用/cmd时的回复

//1.定义命令规则
export class kelitaocan extends plugin {
  constructor() {
    super({
      name: "终端",
      dsc: "终端",
      event: "message",
      priority: 600,
      rule: [
        {
          reg: '^/cmd.+$',
          fnc: 'zhongduan'
        },
        {
          reg: '^/cmd$',
          fnc: 'cmd'
        },
        {
          reg: '^#?(command|终端)(help|帮助|菜单|功能)',
          fnc: 'help'
         }
          ],
    })
  }
async help(e) {
  if (e.isMaster) {
    await this.reply(`/cmd帮助：\n单条命令执行：\n/cmd [命令]\n多条命令执行：\n/cmd [命令1] && [命令2]  && [命令3] && ......\n\n注意：\n1.每次执行/cmd命令后都会回到/root\n2.该插件具有一定破坏性，所以仅“${botname}”的主人可用\n3.部分变量无法使用甚至报错，这是正常的`);
    } else {
    await this.reply(`${no}`);
    return;
    }
}
async cmd(e) {
  if (e.isMaster) {
    await this.reply(`没有命令${botname}怎么执行啊~`);
    } else {
    await this.reply(`${no}`);
    return;
    }
}
async zhongduan(e) {
  if (!e.isMaster) {
     await this.reply(`${no}`);
    return;
    }
  //执行的逻辑功能
  let msg = e.msg.replace("/cmd","");
  var exec = require('child_process').exec;
  var ls = exec('cd ../ &&'+msg, function (error, stdout, stderr){
    if (error) {  //报错内容改为容易解读的常规输出
      e.reply("[root@localhost]#"+msg+"\n"+"失败！错误代号: "+error.code+"\n"+error.message+"-----------------------\nYunzai-Bot & xiaoyue-plugin");
    }else{
      e.reply("[root@localhost]#"+msg+"\n"+stdout+"-----------------------\nYunzai-Bot & xiaoyue-plugin")
    }
  })

  return true; //返回true 阻挡消息不再往下
  }
}
